lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'upload'], {
    data: {
        sellistinfo: {},//选中列表行 
        table: null,
        tabledata: [],
        // listmodel: {},//列表行模型
        // paytype: [],//支付类型
        // paymentlist: [],//支付方式
        // pwdmodel: {},
    },
    pageload: function (right) {
        console.log(right);
        var self = this;
        this.initevent();
        self.tableLoad();
        self.initEvent();
    },
    initevent() {

    },
    tableLoad: function () {
        var self = this;
        var subMchId = $("#SubMchId").val();
        self.data.table = layui.table.render({
            elem: "#dataTable",
            where: {
                searchkey: subMchId
            },
            headers: lobsterlayui.GetHeaderData(),
            toolbar: '#mchadd',
            url: lobsterlayui.GetLowCodeUrl('tspay.getmchlist'),
            height: "full-100",
            size: '10',
            page: true, //开启分页 
            loading: true,
            cols: [[
                { field: 'SubMchId', title: '商户号', width: '15%' },
                { field: 'SubMchName', title: '商户名称', width: '15%' },
                { field: 'Purpose', title: '用途', width: '20%' },
                { field: 'Keys', title: '验证key', width: '15%' },
                { field: 'operation', title: '操作', toolbar: '#edit', width: '35%' }
            ]]
            , done: function (res) {

            },
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.data
                }
            }
        });
    },
    //打开模板编辑框
    openEdit(title) {
        var self = this;
        layer.open({
            type: 2
            , title: title
            , content: "edit.html?appid=" + lobsterlayui.GetAppId()
            , area: ['50%', '80%']
            , btn: ['保存', '取消']
            , resize: false
            , success: function (layero, index) {

            }
            , btn2: function (index, layero) {

            }
            , cancel: function () {

            }
            , yes: function (index, layero) {
                var a = window.getResult();
                if (a != null) {
                    // self.data.listmodel = a;
                    layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    self.save(a, () => {
                        self.data.table.reload();
                        layer.close(index);
                    })
                }
            }
        });
    },
    //支付配置编辑
    openPaymentEdit: function (title) {
        var self = this;
        layer.open({
            type: 2
            , title: title
            , content: "payment.html?appid=" + lobsterlayui.GetAppId()
            , area: ['100%', '100%']
            , btn: ['保存', '取消']
            , resize: false
            , success: function (layero, index) {

            }
            , btn2: function (index, layero) {

            }
            , cancel: function () {

            }
            , yes: function (index, layero) {
                var a = getResult()
                if (a != null) {
                    // self.data.paymentlist = a;
                    layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    self.savepayment(a, () => {
                        self.tableLoad();
                        layer.close(index);
                    })
                }
            }
        });
    },
    //密码设置编辑
    openPwdEdit: function (title) {
        var self = this;
        layer.open({
            type: 2
            , title: title
            , content: "pwdedit.html?appid=" + lobsterlayui.GetAppId()
            , area: ['50%', '50%']
            , btn: ['保存', '取消']
            , resize: false
            , success: function (layero, index) {

            }
            , btn2: function (index, layero) {

            }
            , cancel: function () {

            }
            , yes: function (index, layero) {
                var a = window.getResult()
                if (a != null) {
                    // self.data.pwdmodel = a;
                    layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    self.savepwd(a, () => {
                        self.tableLoad();
                        layer.close(index);
                    })
                }
            }
        });
    },
    //页面加载
    initEvent: function () {
        var self = this;
        //模板新增
        layui.table.on("toolbar(dataTable)", function (obj) {
            window.sellistinfo = null;
            //新增
            if (obj.event == "add") {
                self.openEdit("商户新增");
            }
        });
        //模板编辑，删除
        layui.table.on('tool(dataTable)', function (obj) {
            var data = obj.data;//当前行数据
            self.data.sellistinfo = data;
            switch (obj.event) {
                case 'edit':
                    window.sellistinfo = self.data.sellistinfo;
                    self.openEdit(data.SubMchName + '-编辑');
                    break;
                case 'enstatus':
                    var tip = "确认启用吗?"
                    if (data.DelFlag == 0) {
                        tip = "确认停用吗?"
                    }
                    layer.confirm(tip, { icon: 3, title: '提示' }, function (index) {
                        layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                        self.enable(data, () => {
                            self.tableLoad();
                            layer.close(index);
                        })
                    })
                    break;
                case 'paymentedit':
                    window.sellistinfo = self.data.sellistinfo;
                    // window.paytype = self.data.paytype;
                    self.openPaymentEdit(data.SubMchName + '-支付编辑');
                    break;
                case 'pwdedit':
                    window.sellistinfo = self.data.sellistinfo;
                    self.openPwdEdit(data.SubMchName + '-密码设置');
                    break;
            }
        });
        //查询列表
        layui.form.on("submit(LAY-list-submit)", function (data) {
            self.tableLoad();
        });
    },
    save: function (model, succ) {
        var self = this;
        lobsterlayui.POSTAPI('tspay.savemch', model).then(retdata => {
            layer.msg("保存成功!")
            succ && succ();
        }).catch(res => {
            layer.msg(res.msg)
        })
    },
    savepayment: function (paymentlist, succ) {
        var self = this;
        lobsterlayui.POSTAPI('tspay.savepayment', { paymentlist: JSON.stringify(paymentlist) }).then(retdata => {
            layer.msg("保存成功!")
            succ && succ();
        }).catch(res => {
            layer.msg(res.msg);
        })
    },
    savepwd: function (pass, succ) {
        var self = this;
        lobsterlayui.POSTAPI('tspay.setpwd', pass).then(retdata => {
            layer.msg("保存成功!")
            succ && succ();
        }).catch(res => {
            layer.msg(res.msg);
        })
    },
    /**
     * 停启用分润规则
     * */
    enable: function (data, succ) {
        var delFlag = data.DelFlag;
        if (delFlag == 0)
            delFlag = 1;
        else
            delFlag = 0;
        lobsterlayui.GETAPI('tspay.setdelflag', { id: data.ID, status: delFlag }).then(retdata => {
            layer.msg("保存成功!")
            succ && succ();
        }).catch(res => {
            layer.msg("保存失败!")
        })
    },
})

